﻿using Prism.Commands;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApp_PrismRegionBase.ViewModels.Pages;


public class ViewBViewModel : INavigationAware,IRegionMemberLifetime,IConfirmNavigationRequest,IJournalAware
{
    public string Title { get; set; } = "View B";

    public string? TextValue { get; set; }

    public DelegateCommand? CloseNow { get; set; }

    public DelegateCommand? CloseAll { get; set; }

    public DelegateCommand<string>? NavForwardDeleateCommand { get; set; }

    /// <summary>
    /// 导航日志
    /// </summary>
    private IRegionNavigationJournal? regionNavigationJournal;

    public ViewBViewModel(IRegionManager regionManager)
    {
        CloseNow = new DelegateCommand(() => 
        {
            var region = regionManager.Regions["viewRegion"];
            var _views = region.Views.FirstOrDefault(f => f.GetType().Name == "ViewB");
            if(_views != null ) 
            {
                region.Remove(_views);
            }
            
        });

        CloseAll = new DelegateCommand(() => 
        {
            var region = regionManager.Regions["viewRegion"];
            region.RemoveAll();
        });

        NavForwardDeleateCommand = new DelegateCommand<string>(_arg =>
        {
            if (regionNavigationJournal == null)
                return;
            if (_arg == "Forward")
            {
                if (regionNavigationJournal.CanGoForward)
                    regionNavigationJournal.GoForward();
                else
                    regionManager.RequestNavigate("viewRegion", "ViewC");
            }
            else
            {
                if (regionNavigationJournal.CanGoBack)
                    regionNavigationJournal.GoBack();
                else
                    regionManager.RequestNavigate("viewRegion", "ViewA");
            }
        });
    }

    #region 接口 IRegionMemberLifetime 页面生命周期

    public bool KeepAlive => true; //默认true    是否再region保留

    #endregion

    #region 接口 INavigationAware 观察导航过程，接收参数

    public bool IsNavigationTarget(NavigationContext navigationContext)
    {
        // 返回true:从A -> B -> A(显示第一个A)
        // 返回false:从A -> B -> A(显示新对象)

        return true;
    }

    /// <summary>
    /// 从当前页面导航到别的页面中去
    /// </summary>
    /// <param name="navigationContext"></param>
    public void OnNavigatedFrom(NavigationContext navigationContext)
    {
        //从这里导航到ViewC中可以把参数带过去 但是 OnNavigatedTo 中的参数带不过去
        navigationContext.Parameters.Add("password", "a");
    }

    /// <summary>
    /// 从别的页面导航过来
    /// </summary>
    /// <param name="navigationContext"></param>
    public void OnNavigatedTo(NavigationContext navigationContext)
    {
        regionNavigationJournal = navigationContext.NavigationService.Journal;
        //接收参数
        string _username = navigationContext.Parameters.GetValue<string>("username");
        Console.WriteLine(_username);
    }

    #endregion

    #region 接口 IConfirmNavigationRequest 是否允许从当前页面导航出去
    public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
    {
        if (MessageBox.Show("是否退出当前页面?", "系统消息", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
        {
            continuationCallback?.Invoke(true);
        }
        else 
        {
            continuationCallback?.Invoke(false);
            MessageBox.Show("拒绝导航！");
        }
    }


    #endregion

    #region 接口 IJournalAware 导航日志是否记录
    public bool PersistInHistory()
    {
        return false;
    }

    #endregion
}
